
package de.fhhof.brst.user;

import java.io.Serializable;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Session;
import de.fhhof.brst.dao.HibernateUserDAO;
import de.fhhof.brst.dao.HibernateUtil;
import de.fhhof.brst.dao.UserDAO;
import de.fhhof.brst.data.User;
import de.fhhof.brst.exceptions.UserAlreadyExistsException;
@ManagedBean
@ViewScoped
public class Register implements Serializable {
  /**
   * 
   */
  private static final long serialVersionUID =  -6067329034861339945L;

  private de.fhhof.brst.data.User user;

  private de.fhhof.brst.dao.UserDAO userDAO =  HibernateUserDAO.getHibernateUserDAO();

  private String confirmPass =  "";

  public Register() {
		user = new User();
		user.setAge(new Date("01/01/1980"));
  }

  public String submit() {
		String returnMessage = "";
		FacesMessage message = new FacesMessage("Registration succesful!");

		if (!this.confirmPass.equals(this.user.getPassword())) {
			this.confirmPass = "";
			message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"Passwort falsch!", "Nochmal versuchen");
		} else {
			try {
				userDAO.registerUser(user);
				returnMessage = "/login_frame.xhtml?faces-redirect=true";
			} catch (UserAlreadyExistsException e) {
				message = new FacesMessage("Registration unsuccesful!");
			}
		}

		FacesContext.getCurrentInstance().addMessage(null, message);

		return returnMessage;
  }

  public de.fhhof.brst.data.User getUser() {
		return user;
  }

  public String getConfirmPass() {
		return confirmPass;
  }

  public void setConfirmPass(String confirmPass) {
		this.confirmPass = confirmPass;
  }

}
